package com.phamloc.shootingchicken.scene;

import org.andengine.engine.camera.Camera;
import org.andengine.entity.scene.menu.MenuScene;
import org.andengine.entity.scene.menu.MenuScene.IOnMenuItemClickListener;
import org.andengine.entity.scene.menu.item.IMenuItem;
import org.andengine.entity.scene.menu.item.decorator.ScaleMenuItemDecorator;
import org.andengine.entity.sprite.Sprite;
import org.andengine.opengl.util.GLState;

import com.phamloc.shootingchicken.GameActivity;
import com.phamloc.shootingchicken.base.BaseScene;
import com.phamloc.shootingchicken.extra.AudioPlayer.MusicKeys;
import com.phamloc.shootingchicken.manager.SceneManager;
import com.phamloc.shootingchicken.manager.SceneManager.SceneType;
import com.phamloc.shootingchicken.objects.SpriteMenuItemWithText;
import com.phamloc.shootingchicken.objects.ToggleButtonSprite;

public class MainMenuScene extends BaseScene implements
		IOnMenuItemClickListener {
	private MenuScene menuChildScene;
	private final int MENU_PLAY = 0;
	private final int MENU_SCORE = 1;
	private final int MENU_EXIT = 2;

	@Override
	public void createScene() {
		createBackground();
		createMenuChildScene();
		if(resourcesManager.audioPlayer != null)
			resourcesManager.audioPlayer.play(MusicKeys.GAME_MUSIC);
	}

	private void createMenuChildScene() {
		menuChildScene = new MenuScene(camera);
		menuChildScene.setPosition(0, 25);
		final IMenuItem playMenuItem = new ScaleMenuItemDecorator(
				new SpriteMenuItemWithText(MENU_PLAY,
						resourcesManager.menuRegion.getTextureRegion(0), vbom,
						"Play", resourcesManager.font), 1.2f, 1f);
		final IMenuItem scoreMenuItem = new ScaleMenuItemDecorator(
				new SpriteMenuItemWithText(MENU_SCORE,
						resourcesManager.menuRegion.getTextureRegion(0), vbom,
						"Score", resourcesManager.font), 1.2f, 1f);
		final IMenuItem exitMenuItem = new ScaleMenuItemDecorator(
				new SpriteMenuItemWithText(MENU_EXIT,
						resourcesManager.menuRegion.getTextureRegion(0), vbom,
						"Exit", resourcesManager.font), 1.2f, 1f);
		menuChildScene.addMenuItem(playMenuItem);
		menuChildScene.addMenuItem(scoreMenuItem);
		menuChildScene.addMenuItem(exitMenuItem);
		playMenuItem.setPosition(0, 0);
		scoreMenuItem.setPosition(0, playMenuItem.getY() - 110);
		exitMenuItem.setPosition(0, scoreMenuItem.getY() - 110);
		menuChildScene.buildAnimations();
		menuChildScene.setBackgroundEnabled(false);
		menuChildScene.setOnMenuItemClickListener(this);
		
		ToggleButtonSprite soundButton = new ToggleButtonSprite(exitMenuItem.getWidth() + 35, exitMenuItem.getHeight() - 53, resourcesManager.menuButtonRegion.getTextureRegion(0), vbom);
		setChildScene(menuChildScene);
		soundButton.setScale(0.3f);
		soundButton.setAudioPlayer(resourcesManager.audioPlayer);
		getChildScene().attachChild(soundButton);
		getChildScene().registerTouchArea(soundButton);
	}

	private void createBackground() {
		Sprite backgroud = new Sprite(GameActivity.SCREEN_WIDHT / 2,
				GameActivity.SCREEN_HIGHT / 2, resourcesManager.menuBackgroud,
				vbom) {
			@Override
			protected void preDraw(GLState pGLState, Camera pCamera) {
				super.preDraw(pGLState, pCamera);
				pGLState.enableDither();
			}
		};
		backgroud.setScale(1.1f);
		attachChild(backgroud);
	}

	@Override
	public void onBackKeyPressed() {
		System.exit(0);
	}

	@Override
	public SceneType getSceneType() {
		return SceneType.SCENE_MENU;
	}

	@Override
	public void disposeScene() {
	}

	@Override
	public boolean onMenuItemClicked(MenuScene pMenuScene, IMenuItem pMenuItem,
			float pMenuItemLocalX, float pMenuItemLocalY) {
		switch (pMenuItem.getID()) {
		case MENU_PLAY:
			SceneManager.getInstance().loadGameScene(engine);
			return true;
		case MENU_SCORE:

			return true;
		case MENU_EXIT:
			System.exit(0);
			return true;
		default:
			return false;
		}
	}

}
